
$(function() {
	
	Tip.emp('负责人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("负责人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});

	$("select[id^='way_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
	
	$("select[id^='type_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
});

var Requirement = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
							
				var form = $("#form_add")[0];

				var body = { };
				
				body.name = form.name.value;
				body.type = form.type.value;
				body.way = form.way.value;
				body.payment = form.payment.value;								
				
				body.address = form.address.value;
				body.recipient = form.recipient.value;
				body.mobile = form.mobile.value;
				
				body.measure = form.measure.value;
				body.quantity = form.quantity.value;
				
				body.weight1 = form.weight1.value;
				body.weight2 = form.weight2.value;
							
				body.content = form.content.value;
				
				body.mold = form.mold.value;
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				$(this).dialog("close");

				$.post('express.php?do=Requirement.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},	

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'express.php?do=Requirement.edit&id=' + id;
		var c2 = 'express.php?do=Requirement.view&id=' + id;
		var c3 = 'express.php?do=Requirement.copy&id=' + id;
		
		var cs = 'express.php?do=Requirement.status&id=' + id;
		
		var c4 = 'express.php?do=Requirement.plan&id=' + id;
		var c5 = 'express.php?do=Requirement.reason&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'Requirement.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		
		var ac = ['复制信息', 'url', 'Requirement.dialog_copy_', '650x400'];
		
		var a5 = ['暂停货运', 'url', 'Requirement.dialog_stop', '650x400'];
		var a6 = ['取消货运', 'url', 'Requirement.dialog_cancel', '650x400'];
		
		// 用车申请
		if (status == 1) {
			var d1 = 'express.php?do=Requirement.verify&id=' + id;
			var b1 = ['审核信息', 'url', 'Requirement.dialog_verify', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}

		// 等待安排
		if (status == 2) {
			var d2 = 'express.php?do=Requirement.freight&id=' + id;
			
			var b1 = ['货运安排', 'url', 'Requirement.dialog_plan', '650x400'];
			var b2 = ['直接安排', 'url', 'Requirement.dialog_freight', '650x400'];
			
			var s = c4 + ", " + d2 + ", " + c1 + ", " + c2 + ", " + c5 + ", " + c5;

			return this.popup_(e, [b1, b2, a1, a2, a5, a6], s);
		}

		// 已经安排
		if (status == 3) {
			var b1 = ['重新安排', 'url', 'Requirement.dialog_plan', '650x400'];
			
			var s = c4 + ", " + c1 + ", " + c2 + ", " + c3 + ", " + c5 + ", " + c5;

			return this.popup_(e, [b1, a1, a2, ac, a5, a6], s);
		}
		
		// 暂停安排
		if (status == 5) {
			var d1 = 'express.php?do=Requirement.status_continue&id=' + id + p2;
			var b1 = ['继续安排', 'url', 'Requirement.list_continue'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}
		
		// 取消安排
		if (status == 6) {
			var d1 = 'express.php?do=Requirement.status_restart&id=' + id + p2;
			var b1 = ['重启安排', 'url', 'Requirement.list_restart'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}
			
		var s = c2 + ", " + c3;

		return this.popup_(e, [a2, ac], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c2 = 'express.php?do=Requirement.view&print=print&id=' + id;
		var a2 = ['打印信息', 'url', 'Common.dialog_view_', '742x508'];
		
		var ss = 'express.php?do=Requirement.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Requirement.dialog_ss', '400x200'];
		
		s += ", " + c2 + ", " + ss;
		
		a.push(a2);
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Plan.audit&item=140', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'express.php?do=Requirement.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() {
						$(this).dialog("close");
					},
			
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
			
						var body = {};
						body.id = form.id.value;
						body.name = form.name.value;
						body.type = form.type.value;
						body.way = form.way.value;
						body.payment = form.payment.value;				
						
						body.address = form.address.value;
						body.recipient = form.recipient.value;
						body.mobile = form.mobile.value;
						
						body.measure = form.measure.value;
						body.quantity = form.quantity.value;
						
						body.weight1 = form.weight1.value;
						body.weight2 = form.weight2.value;
									
						body.content = form.content.value;
						
						body.mold = form.mold.value;
						body.emp = form.emp.value;
						body.date = form.date.value;
			
						$(this).dialog("close");
			
						var url = 'express.php?do=Requirement.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 复制
	 */
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.name = form.name.value;
				body.type = form.type.value;
				body.way = form.way.value;
				body.payment = form.payment.value;				
				
				body.address = form.address.value;
				body.recipient = form.recipient.value;
				body.mobile = form.mobile.value;
				
				body.measure = form.measure.value;
				body.quantity = form.quantity.value;
				
				body.weight1 = form.weight1.value;
				body.weight2 = form.weight2.value;
							
				body.content = form.content.value;
				
				body.mold = form.mold.value;
				body.emp = form.emp.value;
				body.date = form.date.value;

				$(this).dialog("close");

				var url = 'express.php?do=Requirement.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.audit&item=140', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否不同意？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");
						
						var url = 'express.php?do=Requirement.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");
						
						var url = 'express.php?do=Requirement.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");

						var url = 'express.php?do=Requirement.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 6;

						$(this).dialog("close");

						var url = 'express.php?do=Requirement.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 货运安排
	 */
	dialog_plan: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.audit&item=141', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.plan = form.plan.value;
						body.content = form.content.value;
						
						$(this).dialog("close");

						var url = 'express.php?do=Requirement.planPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 直接货运
	 */
	dialog_freight: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.audit&item=141', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						body.way = form.way.value;
						body.content = form.content.value;
						
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
											
						$('#dialog_add').dialog("close");
						
						var url = 'express.php?do=Plan.freightPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$.post(url, body, function(data) {
							Requirement.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_restart: function (url, view) {
		$.getJSON('express.php?do=Requirement.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要重启派车？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('express.php?do=Requirement.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要继续派车？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	plan: function (id) {
		var url = "express.php?do=Plan.view&id=" + id;
		Common.dialog_view_(url, '货运安排', '800x500');
	},

	list_: function () {
		var result = Common.result();
		var url = 'express.php?do=Requirement.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	},

	all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'express.php?do=Requirement.delete' + p2;
		var c2 = 'express.php?do=Requirement.batch_verify';
		var c3 = 'express.php?do=Requirement.batch_record';
		var c4 = 'express.php?do=Requirement.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['审核信息', 'url', 'Requirement.dialog_batch_verify', '800x450', '审核信息'];
		var a3 = ['安排货运', 'url', 'Requirement.dialog_batch_record', '650x420', '安排货运'];
		
		var a4 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c4;

		return popup(e, [a1, a4], s);
	},

	dialog_batch_verfity: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.audit&item=140', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_1_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否审核不通过？")) {
							return false;
						}
						
						var cc = _cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;
						body.status = 9;
						
						var url = 'express.php?do=Requirement.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"安排采购": function() {
						if (!confirm("确认是否安排采购？")) {
							return false;
						}
						
						var cc = _cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;					
						body.status = 2;
						
						var url = 'express.php?do=Requirement.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"使用库存": function() {
						if (!confirm("确认是否使用库存？")) {
							return false;
						}

						var cc = _cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;					
						body.status = 8;
						
						var url = 'express.php?do=Requirement.batch_verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},

	dialog_batch_record: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Requirement.audit&item=141', function(json) {
			if (json.status == 1) {
				var c = new Array();
				$("input[name^='c_2_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var cc = _cc();

						var form = $("#form_add")[0];
						
						var body = {};
						body.cc = cc;
						body.purchase = form.purchase.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'express.php?do=Requirement.batch_recordPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_add';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});
	}
};
